﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace Editor.ProjectDanmaku.EditorWindow
{
    public class CoordUtilityWindow : UnityEditor.EditorWindow
    {
        private Texture2D _tex;
        private Vector2 _scrollPos;
        
        private List<Vector2> _rawCoords = new List<Vector2>();
        private List<Vector2> _coords = new List<Vector2>();
        
        [MenuItem("工具/坐标拾取器")]
        private static void ShowWindow()
        {
            var window = GetWindow<CoordUtilityWindow>();
            window.titleContent = new GUIContent("坐标拾取器");
            window.Show();
        }

        private void OnEnable()
        {
            _tex = new Texture2D(16 * 30, 9 * 30, TextureFormat.ARGB32, false);
            _rawCoords = new();
            _coords = new();

            ResetTex();
        }

        private void ResetTex()
        {
            for (int i = 0; i < _tex.width; i++)
            {
                for (int j = 0; j < _tex.height; j++)
                {
                    _tex.SetPixel(i, j, Color.black);
                }
            }
            _tex.Apply();
        }

        private void OnDisable()
        {
            _rawCoords = null;
            _coords = null;
            DestroyImmediate(_tex, true);
        }

        private void RecordPos(Vector2 pos)
        {
            Debug.Log(pos.ToString());
            
            _rawCoords.Add(pos);

            for (int x = 0; x < 16 * 30; ++x)
            {
                _tex.SetPixel(x, 9 * 30 - (int) pos.y, Color.white);
            }
            for (int y = 0; y < 9 * 30; ++y)
            {
                _tex.SetPixel((int) pos.x, y, Color.white);
            }
            
            _tex.Apply();
            
            pos = (pos / new Vector2(16 * 30, 9 * 30)) * new Vector2(16, 9);
            pos.y = 9 - pos.y;
            
            _coords.Add(pos);
        }

        private void OnGUI()
        {
            var mousePos = Event.current.mousePosition;
            
            GUILayout.Label($"Current Mouse Pos: {mousePos.x}, {mousePos.y}");
            GUILayout.Space(10);
            
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Reset", GUILayout.ExpandWidth(false)))
            {
                ResetTex();
                _rawCoords.Clear();
                _coords.Clear();
            }
            
            GUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            
            GUILayout.FlexibleSpace();
            
            var panelRect  = GUILayoutUtility.GetRect(16 * 30, 9 * 30);
            
            if (GUI.Button(panelRect, _tex, new GUIStyle("Label")))
            {
                RecordPos(mousePos - panelRect.position);
            }
            
            GUILayout.Space(5);

            _scrollPos = EditorGUILayout.BeginScrollView(
                _scrollPos, GUILayout.Width(200), GUILayout.Height(270));
            
            foreach (var coord in _coords)
            {
                EditorGUILayout.LabelField($"{coord.x}, {coord.y}");
            }
            
            EditorGUILayout.EndScrollView();
            
            GUILayout.FlexibleSpace();
            
            EditorGUILayout.EndHorizontal();
        }
    }
}